import Vue from 'vue'
import VueRouter from 'vue-router'

//view
import Home from '../components/common/Home.vue'
import Login from '../components/blocks/Login.vue'
import Welcome from '../components/blocks/view/Welcome.vue'
import Orders from '../components/blocks/view/Orders.vue'
import Foods from '../components/blocks/view/Foods.vue'
import Members from '../components/blocks/view/Members.vue'
import Business from '../components/blocks/view/Business.vue'
import Category from '../components/blocks/view/Category'

//action
import AddFoods from '../components/blocks/action/AddFoods'
import EditFoods from '../components/blocks/action/EditFoods'
import ViewFoods from '../components/blocks/action/ViewFoods'
import ViewOrders from '../components/blocks/action/ViewOrders'



Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        redirect: '/welcome',
        meta: ['首页'],
        children: [{
                path: '/welcome',
                component: Welcome,
                meta: ['首页']
            },
            {
                path: '/orders',
                name: 'Orders',
                component: Orders,
                meta: ['订单管理', '订单列表'],
            },
            {
                path:'/vieworders',
                name:'ViewOrders',
                component:ViewOrders,
                meta:['订单管理','订单列表','订单详情']
            },
            {
                path: '/foods',
                name: 'Foods',
                component: Foods,
                meta: ['商品管理', '商品列表'],
            },
            {
                path: '/addfoods',
                name: 'AddFoods',
                component: AddFoods,
                meta: ['商品管理', '添加商品'],
            },
            {
                path: '/viewfoods',
                name: 'ViewFoods',
                component: ViewFoods,
                meta: ['商品管理', '商品列表', '查看商品'],
            },
            {
                path: '/category',
                name: 'Category',
                component: Category,
                meta: ['商品管理', '分类列表'],
            },
            {
                path: '/editfoods',
                name: 'EditFoods',
                component: EditFoods,
                meta: ['商品管理', '商品列表', '编辑商品'],
            },
            {
                path: '/members',
                name:'Members',
                component: Members,
                meta: ['会员管理', '会员列表'],
            },
            {
                path: '/business',
                name:'Business',
                component: Business,
                meta: ['商务合作', '发布推文'],
            }
        ]
    }, {
        path: '/login',
        name: 'Login',
        component: Login
    }
]

const router = new VueRouter({
    routes,
    // mode: 'history',
    // base: process.env.BASE_URL,
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to将要访问的路径
    // from从哪个路径跳转而来
    // next 是一个函数 表示放行
    // next() 放行 next('/login) 强制跳转
    if (to.path === '/login') return next();
    // 获取token
    const tokenStr = window.localStorage.getItem('admin_token');
    if (!tokenStr) return next('/login');
    next();
})

export default router